package com.tduck.cloud.account.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tduck.cloud.account.entity.LoginFailEntity;
import com.tduck.cloud.account.entity.UserEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 用户(AcUser)表数据库访问层
 *
 * @author smalljop
 * @since 2020-11-10 18:10:41
 */
public interface UserMapper extends BaseMapper<UserEntity> {

    /**
     * 登录失败，记录信息
     * */
    @Insert("insert into AC_USER_LOGIN_FAIL (ID, LOGIN_TIME, DELETED, LOCKED, UNLOCK_TIME) values (#{id, jdbcType=VARCHAR}, #{loginTime, jdbcType=TIMESTAMP}, #{deleted, jdbcType=INTEGER}, #{locked, jdbcType=INTEGER}, #{unlockTime, jdbcType=TIMESTAMP})")
    int recordLoginFail(LoginFailEntity loginFailEntity);


    /**
     * 查询登录次数
     * */
    @Select("SELECT count(*) as num\n" +
            "FROM AC_USER_LOGIN_FAIL\n" +
            "WHERE ID = #{id,}\n" +
            "  AND DELETED = 0")
    int queryLoginCount(Long id);

    /**
     * 查询登录信息
     * */
    @Select("SELECT ID as id, LOGIN_TIME as loginTime, DELETED as deleted, LOCKED as locked, UNLOCK_TIME as unlockTime\n" +
            "FROM AC_USER_LOGIN_FAIL\n" +
            "WHERE ID = #{id, jdbcType=VARCHAR}\n" +
            "  AND DELETED = 0\n" +
            "  AND LOCKED = 1\n" +
            "  ORDER BY UNLOCK_TIME DESC")
    List<LoginFailEntity> queryLockedInfo(Long id);

    /**
     * 重置该账号登陆失败信息
     * */
    @Update("update AC_USER_LOGIN_FAIL set DELETED = 1, UPDATE_TIME = sysdate where ID = #{id, jdbcType=VARCHAR} AND DELETED = 0")
    void resetLoginFail(Long id);
}